<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/23
 * Time: 11:23
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;

class PatientDiabetesController extends AdminBaseController
{
    //获取糖尿病检测列表
    public function index()
    {
        $user_id = $this->request->param('id', '', 'intval');
        $start_time = $this->request->param('start_time', '');
        $end_time = $this->request->param('end_time', '');

        $where = array('is_del'=>0);
        $data = array();
        if (!empty($start_time) && !empty($end_time)){
            $where['create_time'] = array('between', [strtotime($start_time),strtotime($end_time)]);
            $data['start_time'] = $start_time;
            $data['end_time'] = $end_time;
        }elseif (!empty($start_time)){
            $where['create_time'] = array('gt', strtotime($start_time));
            $data['start_time'] = $start_time;
        }elseif (!empty($end_time)){
            $where['create_time'] = array('lt', strtotime($end_time));
            $data['end_time'] = $end_time;
        }

        $userInfo = db('patient')->find($user_id);

        $list = db('patient_diabetes')->order('create_time desc')->where($where)->where('user_id', $user_id)->paginate(20);

        $page = $list->render();
        return $this->fetch('', array(
            'list' => $list,
            'userInfo' => $userInfo,
            'page' => $page,
            'where' => $data
        ));
    }

    //查看详情
    public function info()
    {
        $id = $this->request->param('id', '', 'intval');
        $diabetesInfo = db('patient_diabetes')->find($id);
        $userInfo = db('patient')->find($diabetesInfo['user_id']);

        //字段处理
        //是三维数组的的字段
        $arr = ['shenbingbingbian','dianxingzhengzhuang','xinnaoxueguanbingbian','shenjingxingbingbian',
            'yanbingbingbian','riqi','canqianxuetang','canhouxuetang_two','huatangfangyongliang','yaowufuyongliang',
            'yidaosuyongliang_two','xiaoyufangyongliang'];
        foreach($arr as $k=>$v){
            $diabetesInfo[$v] = explode('--',$diabetesInfo[$v]);
        }

        $arr1 = count($diabetesInfo['riqi']);

        //获取图片信息
        $imgs = db('imgs')->where('p_id', $diabetesInfo['id'])->select();

        return $this->fetch('', array(
            'diabetesInfo' => $diabetesInfo,
            'userInfo' => $userInfo,
            'imgs' => $imgs,
            'count' => $arr1
        ));
    }

    public function delete()
    {
        $id = $this->request->param('id', '', 'intval');
        $result = db('patient_diabetes')->where('id', $id)->update(array('is_del'=>1));
        if (!$result){
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

}